#! /usr/bin/python

class OpenSocialClientError(Exception):
  pass


PEOPLE = 'http://ns.opensocial.org/people/0.8',
ACTIVITIES = 'http://ns.opensocial.org/activities/0.8',
APPDATA = 'http://ns.opensocial.org/appdata/0.8',


class ContainerClientConfig(object):
  def __init__(self):
    pass


class UserContextConfig(object):
  def __init__(self):
    pass


class ContainerClient(object):
  """A client to an OpenSocial container.

  """
  def __init__(self, container_config):
    self._config = container_config

  def MakeUserContext(self, user_config):
    return UserContext(user_config, self)

  def PerformOperation(self, user_context, service, params, method='GET',
                       data=None):
    """Perform an OpenSocial operation.

    service: str, a key of KNOWN_SERVICES or a url to a service description.
    params: dict of str->stringable, a mapping to use in the service's
            uri-template to create the uri.
    method: str, http method to use.
    data:  dict of str->str; only valid if method is POST

    returns: the results of the operation
    """
    # TODO(dbentley): implement this
    pass

  # What follows are convenience methods implemented on top of the above
  # infrastructure.
  def FriendsService(self, user_context, method='GET', data=None, guid='',
                     selector='', prefix='', pid='', **kwargs):
    """Access the Friends Service
    """
    params = {}
    if not guid:
      guid = '@me'
    params['guid']=guid
    params['selector']=selector
    params.update(kwargs)
    return self.PerformOperation(user_context, PEOPLE, params, method=method,
                                 data=data)

  def GetFriends(self, user_context, guid):
    return self.FriendsService(user_context, guid=guid)


class UserContext(object):
  """A context for one user in which OpenSocial requests can be made.

  """
  def __init__(self, user_config, container):
    """
    Params:
      user_config: a UserConfig, the configuration for this user
      container: a ContainerClient; the container on which we want to make
                 requests for this user.
    """
    self._config = user_config
    self._container = container

  def PerformOperation(self, *args, **kwargs):
    return self._container.PerformOperation(self, *args, **kwargs)

  def FriendService(self, *args, **kwargs):
    return self._container.FriendService(self, *args, **kwargs)

  def GetFriends(self, guid):
    return self._container.GetFriends(self, guid)
